var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
		datalist: [],
		pagenum: 1,
		pic:[''],
		pics:[],
		detail_pics:[],
		cindex:0
	},
	_onLoad:function(){
		var that = this
		var id = that.options.id ? that.options.id : ''
		$.get($.pre_url + '/am.php?s=/product/edit',{id:id},function(res){
			var info = res.info;
			if(res.info && res.info.pics){
				var pics = res.info.pics;
				res.pics = pics.split(',');
			}
			if(res.info){
				res.pic = [res.info.pic];
				res.status = res.info.status
			}
			var clist = res.clist
			var cateArr = [];
			var cateidArr = [];
			for(var i in clist){
				cateArr.push(clist[i].name)
				cateidArr.push(clist[i].id);
				if(clist[i].child){
					for(var j in clist[i].child){
						cateArr.push(clist[i].name + ' - ' + clist[i].child[j].name)
					cateidArr.push(clist[i].child[j].id);
						if(clist[i].child[j].child){
							for(var k in clist[i].child[j].child){
								cateArr.push(clist[i].name + ' - ' + clist[i].child[j].name + ' - ' + clist[i].child[j].child[k].name)
								cateidArr.push(clist[i].child[j].child[k].id);
							}
						}
					}
				}
			}
			var glist = [];
			var gids = info.gid;
			if(gids){
				gids = gids.split(',')
			}else{
				gids = [];
			}
			for(var i in res.glist){
				var thisg = res.glist[i]
				if(gids.indexOf(thisg.id)!==-1){
					res.glist[i].checked = true;
				}else{
					res.glist[i].checked = false;
				}
				glist.push(res.glist[i]);
			}
			res.gids = gids
			res.glist = glist
			res.cateArr = cateArr
			res.cateidArr = cateidArr
			if(res.info && res.info.detail_pics){
				var detail_pics = res.info.detail_pics;
				res.detail_pics = detail_pics.split(',');
			}
			that.setData(res)
		})
	},
	cateChange:function(e){
		this.setData({cindex:e.detail.value})
	},
	gidChange:function(e){
		that.setData({gids:e.detail.value})
	},
	statusChange:function(e){
		var st = e.detail.value
		this.setData({status:st})
	},
	subform:function(e){
		var that = this
		var formdata = e.detail.value;
		formdata.status = this.data.status
		if(formdata.name == ''){
			$.error('请输入商品名称');return;
		}
		if(formdata.sell_price == ''){
			$.error('请输入销售价');return;
		}
		if(formdata.weight == ''){
			$.error('请输入商品重量');return;
		}
		if(formdata.stock == ''){
			$.error('请输入商品库存');return;
		}
		if(formdata.buynum == ''){
			$.error('请输入默认购买数量');return;
		}
		if(formdata.pics == ''){
			$.error('请上传商品图片');return;
		}
		var id = that.options.id ? that.options.id : ''
		$.post($.pre_url + '/am.php?s=/product/edit',{id:id,info:formdata},function(res){
			if(res.status==0){
				$.error(res.msg);
			}else{
				$.success(res.msg)
				setTimeout(function(){
					$.goto('index','redirect');
				},1000)
			}
		});
	}
}))